//
//  VehicleMaintenanceCell.m
//  阿帕
//
//  Created 阿帕 on 2020/1/4.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "VehicleMaintenanceCell.h"

@implementation VehicleMaintenanceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    NSArray *titleArr = @[@"车牌号:",@"申请内容:",@"维修类型:",@"申请状态:",@"申请详情:"];
    for (int i=0; i<titleArr.count; i++) {
        int x = i%2;
        int y = i/2;
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(ScreenW/2 * x, 35 * y, ScreenW/2, 35)];
        [self.contentView addSubview:bgView];
        
        CGSize size = [DCSpeedy dc_calculateTextSizeWithText:titleArr[i] WithTextFont:14 WithMaxW:1000];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, size.width, bgView.cy_height)];
        textLabel.text = titleArr[i];
        textLabel.textColor = Dark_TextColor;
        textLabel.font = font(14);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(textLabel.cy_right + 5, 0, ScreenW/2-size.width-5, bgView.cy_height)];
        valueLabel.tag = 100+i;
        valueLabel.text = @"-";
        valueLabel.textColor = Light_TextColor;
        valueLabel.font = font(14);
        valueLabel.numberOfLines = 2;
        valueLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:valueLabel];
        
        if (i == 4) {
            valueLabel.cy_width = ScreenW-size.width-10;
        }
    }
    
    //分割线
    UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 105, ScreenW - 30, 1)];
    hLine.backgroundColor = BackgroundColor;
    [self.contentView addSubview:hLine];
    
    
    UIView *bgButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, hLine.cy_bottom, ScreenW, 40)];
    bgButtonView.tag = 500;
    [self.contentView addSubview:bgButtonView];
    
    
    UIView *bLine = [[UIView alloc] initWithFrame:CGRectMake(0, bgButtonView.cy_bottom, ScreenW, 10)];
    bLine.backgroundColor = BackgroundColor;
    [self.contentView addSubview:bLine];
}

- (void)setModel:(VehicleMaintenanceRecordModel *)model {
    
    _model = model;
    
//    repairType 2 外出维修 1内部车辆 0中途车坏
//    status (integer, optional): 0:审批中 1：已同意 2：已拒绝 ,
    
    NSString *repairType = @"";
    NSString *status = @"";
    
    if (model.repairType == 0) {
        repairType = @"中途车坏";
    } else if (model.repairType == 1) {
        repairType = @"内部车辆";
    } else if (model.repairType == 2) {
        repairType = @"外出维修";
    }
    
    UIView *bgButtonView = [self.contentView viewWithTag:500];
    if (bgButtonView) {
        [bgButtonView removeAllSubviews];
    }
    
    if (model.status == 1) {
        //此时可以上报详情
        status = @"已同意";
       
        NSMutableArray *titleArr = [[NSMutableArray alloc] init];
        NSMutableArray *imgArr = [[NSMutableArray alloc] init];
        
        if (model.reportCompleteFee == 0) {
            //审批通过还未上报
            [titleArr addObject:@"详情"];
            [titleArr addObject:@"上报详情"];
            [imgArr addObject:@"gcy_icon_my_index9"];
            [imgArr addObject:@"gcy_icon_my_index8"];
        } else {
            [titleArr addObject:@"详情"];
            [imgArr addObject:@"gcy_icon_my_index9"];
        }
        //        NSArray *titleArr = @[@"详情", @"上报详情"];
        //        NSArray *imgArr = @[@"gcy_icon_my_index9", @"gcy_icon_my_index8"];
        
        CGFloat width = ScreenW/titleArr.count;
        
        for (int i=0; i<titleArr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(width*i, 0, width, 40);
            [button setTitle:titleArr[i] forState:UIControlStateNormal];
            button.titleLabel.font = font(15);
            button.tag = 100 + i;
            [button setImage:image(imgArr[i]) forState:UIControlStateNormal];
            [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [button addTarget:self action:@selector(buttonTypeClick:) forControlEvents:UIControlEventTouchUpInside];
            [bgButtonView addSubview:button];
            
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
        }
        
    } else {
        
        if (model.status == 0) {
            status = @"审批中";
        }  else if (model.status == 2) {
            status = @"已拒绝";
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, ScreenW, 40);
        [button setTitle:@"详情" forState:UIControlStateNormal];
        button.titleLabel.font = font(16);
        button.tag = 100;
        [button setImage:image(@"gcy_icon_my_index9") forState:UIControlStateNormal];
        [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [button addTarget:self action:@selector(buttonTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgButtonView addSubview:button];
        
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:10];
    }
    
    
    NSArray *arr = @[[NSString nullToString: model.numberPlate],
                     [NSString nullToString: model.title],
                     [NSString nullToString: repairType],
                     [NSString nullToString: status],
                     [NSString nullToString: model.details],
    ];
    
    for (int i=0; i<arr.count; i++) {
        UILabel *textLabel = [self viewWithTag:100 + i];
        textLabel.text = arr[i];
    }
}

- (void)buttonTypeClick:(UIButton *)button {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellButtonIndexClick:withButton:)]) {
           [self.delegate cellButtonIndexClick:button.tag withButton:button];
       }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
